package com.luo.gmall.pms.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.luo.gmall.pms.entity.AttrEntity;
import com.luo.gmall.pms.service.AttrService;
import com.luo.gmall.pms.vo.ItemGroupVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.luo.gmall.pms.entity.AttrGroupEntity;
import com.luo.gmall.pms.service.AttrGroupService;
import com.luo.gmall.common.bean.PageResultVo;
import com.luo.gmall.common.bean.ResponseVo;
import com.luo.gmall.common.bean.PageParamVo;

/**
 * 属性分组
 *
 * @author zoro
 * @email 1160848022@qq.com
 * @date 2021-11-01 19:22:15
 */
@Api(tags = "属性分组 管理")
@RestController
@RequestMapping("pms/attrgroup")
public class AttrGroupController {

    @Autowired
    private AttrGroupService attrGroupService;


    /**
     * 12.根据cid spuId skuId查询规格参数分组及组下的规格参数和值
     */
    @GetMapping("itemGroups/{categoryId}")
    public ResponseVo<List<ItemGroupVo>> queryWithItemGroupByCategoryIdAndSpuIdAndSkuId(
            @PathVariable Long categoryId,
            @RequestParam("spuId") Long spuId,
            @RequestParam("skuId") Long skuId) {
        List<ItemGroupVo> itemGroupVos = attrGroupService.queryWithItemGroupByCategoryIdAndSpuIdAndSkuId(categoryId,spuId,skuId);
        return ResponseVo.ok(itemGroupVos);
    }

    /**
     * 查询分类下的组及规格参数
     *
     * @param categoryId 类别id
     * @return 类别下的列表数据
     */
    @GetMapping("withattrs/{categoryId}")
    public ResponseVo<List<AttrGroupEntity>> getAttrByCatId(@PathVariable("categoryId") Long categoryId) {
        List<AttrGroupEntity> attrGroupEntities = attrGroupService.getAttrByCatId(categoryId);
        return ResponseVo.ok(attrGroupEntities);
    }

    /**
     * 查询三级分类的分组
     *
     * @param cid 分类id
     * @return 返回三级分类列表
     */
    @GetMapping("category/{cid}")
    public ResponseVo<List<AttrGroupEntity>> getAttrGroupListBycid(@PathVariable("cid") Long cid) {
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", cid);
        List<AttrGroupEntity> attrGroupEntities = attrGroupService.list(queryWrapper);
        return ResponseVo.ok(attrGroupEntities);
    }

    /**
     * 列表
     */
    @GetMapping
    @ApiOperation("分页查询")
    public ResponseVo<PageResultVo> queryAttrGroupByPage(PageParamVo paramVo) {
        PageResultVo pageResultVo = attrGroupService.queryPage(paramVo);

        return ResponseVo.ok(pageResultVo);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("详情查询")
    public ResponseVo<AttrGroupEntity> queryAttrGroupById(@PathVariable("id") Long id) {
        AttrGroupEntity attrGroup = attrGroupService.getById(id);

        return ResponseVo.ok(attrGroup);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation("保存")
    public ResponseVo<Object> save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return ResponseVo.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseVo update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return ResponseVo.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseVo delete(@RequestBody List<Long> ids) {
        attrGroupService.removeByIds(ids);

        return ResponseVo.ok();
    }

}
